<?php

class Admini_ManagedayController extends Uuch_ManageController{    
    
    public function init() {
        parent::init();
        $this->appendCss('/css/home/warehouse.css');
        $this->appendCss('/admins/css/common.css');
        $this->appendScript('/admins/js/jquery/jquery-1.7.2.min.js')->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js')->appendScript('/js/uuch.js');
        $this->view->type = "system";
        $this->view->countbillout = Application_Model_wayBillItem::getcountbill(Application_Model_wayBillItem::Interior_Outstorage,null);//今日发货
        $this->view->stype = 1;   
    }
    public function  indexAction()
    {
        $this->view->countuser = Application_Model_User::getcountuser(); //今日注册
        $this->view->countlogin = Application_Model_User::getloginuser();//今日登陆
        $this->view->countingstore = Application_Model_ingstore::getcountingstore();//预报入库
        $this->view->countinstore = Application_Model_StoreItem::getcountinstore(date("Y-m-d"),null);//今日入库
        $this->view->countoutstore = Application_Model_StoreItem::getcountoutstore(date("Y-m-d"),null);//今日出库
         
        $this->view->countagentpayin = Application_Model_AgentPay::getcountagentpay(1);//今日代支付未处理
        $this->view->countagentpayout = Application_Model_AgentPay::getcountagentpay(2);//今日代支付已处理
        
        $this->view->countchargein = Application_Model_topUpAlipay::getcountchargr(0);//今日代付款未处理
        $this->view->countchargeout = Application_Model_topUpAlipay::getcountchargr(10);//今日代付款未处理
        
        $this->view->countbillin = Application_Model_wayBillItem::getcountbill(Application_Model_wayBillItem::Interior_Confrimed,null);//今日未发货
        $this->view->countbillout = Application_Model_wayBillItem::getcountbill(Application_Model_wayBillItem::Interior_Outstorage,null);//今日发货
        $this->view->countbill = Application_Model_wayBillItem::getcountbill('',null);//今日总运单
        
        $this->view->countorder4 = Application_Model_Order::countorder(null,Application_Model_Order::STATUS_SELLER_SENDED);//待入库
        $this->view->stype = 5;     
        
    }
    
    
   
    /**
     * 根据用户id获取用户信息
     */
    public function byuseridAction()
    {
        $userid = $this->_getParam('userid');
        $result = Application_Model_User::load($userid);
        if($result)
        {
            $this->json(array('id'=>$result->id,'username'=>$result->nikename,'storeno'=>$result->storeno));
        }
        else
        {
             $this->json(array('id'=>0));
        }    
    }
    /**
     * 根据用户名称获取用户信息
     */
    public function byusernameAction()
    {
        $username = $this->_getParam('username');
        $result = Application_Model_User::getbynikename($username);
        if($result)
        {
            $this->json(array('id'=>$result->id,'username'=>$result->nikename,'storeno'=>$result->storeno));
        }
        else
        {
             $this->json(array('id'=>0));
        }    
    }
    /**
     * 根据仓库号获取用户信息
     */
    public function bystorenoAction()
    {
        $storeno = $this->_getParam('storeno');
        $result = Application_Model_User::getByStoreNo($storeno);
        if($result)
        {
            $this->json(array('id'=>$result->id,'username'=>$result->nikename,'storeno'=>$result->storeno));
        }
        else
        {
             $this->json(array('id'=>0));
        }    
    }
    
    
   
    
    public function billcountAction()
    {
        $this->setTitle("运单统计");
        $dateinbegin = $this->_getParam("dateinbegin")<=$this->_getParam("dateinend")?$this->_getParam("dateinbegin"):$this->_getParam("dateinend");
        $dateinend = $this->_getParam("dateinbegin")>=$this->_getParam("dateinend")?$this->_getParam("dateinbegin"):$this->_getParam("dateinend");
        $result = Application_Model_wayBillItem::getBillcount($dateinbegin,$dateinend);
        $this->view->countdata = $result;
        $view = $dateinbegin&&$dateinend?$dateinbegin." 至 ".$dateinend:($dateinbegin&&!$dateinend?$dateinbegin:(!$dateinbegin&&$dateinend?$dateinend:date("Y-m")));
        if($this->_getParam("export") == "cvs"){
            header("content-Type:text/html; charset=gbk");
            header("Content-Type: application/vnd.ms-execl");
            header("Content-Disposition: attachment; filename=" . $view . "的运单统计.csv");
            header("Pragma: no-cache");
            header("Expires: 0");
            /* first line */
            echo "时间" . "\t";
            echo "运单总数量" . "\t";
            echo "总预付款" . "\t";
            echo "总实运费" . "\t";
            echo "互联易数量" . "\t";
            echo "互联易金额" . "\t";
            echo "圣达数量" . "\t";
            echo "圣达金额" . "\t";
            echo "\t\n";
            /* start of second line */
            if($result != ''){
                foreach ($result as $key => $Item) {
                    echo date("Y-m-d",$key) . "\t";
                    echo $Item["num"] . "\t";
                    echo $Item["zyfk"] . "\t";
                    echo $Item["zsfk"] . "\t";
                    echo $Item["hlynum"] . "\t";
                    echo $Item["hlyzyfk"] . "\t";
                    echo $Item["sdnum"] . "\t";
                    echo $Item["sdzyfk"] . "\t";
                    echo "\t\n";
                }
            }
            exit;
        }
        $this->view->dateinbegin = $dateinbegin;
        $this->view->dateinend = $dateinend;
        $this->view->nowYm = $view;
        $this->view->stype = 8;
    }
    public function operateAction()
    {
        $this->setTitle("操作日志");
        $pagesize=15;        
        $pageindex = $this->_getParam('start');
        if(!$pageindex){$pageindex=0;}
        $quick = $this->_getParam("quick");
        $dateinbegin = $this->_getParam("dateinbegin");
        $dateinend = $this->_getParam("dateinend");
        $result = Application_Model_Operate::getserFilter($quick,$dateinbegin,$dateinend,$pagesize, $pageindex);
        $this->view->operatelist = $result["list"];
        $this->view->operateall = $result;
        $this->view->quick = $quick;
        $this->view->dateinbegin = $dateinbegin;
        $this->view->dateinend = $dateinend;
        $this->view->stype = 10;
    }

    public function buyshopcountAction()
    {
        $this->setTitle("代购统计");
        $dateinbegin = $this->_getParam("dateinbegin")<=$this->_getParam("dateinend")?$this->_getParam("dateinbegin"):$this->_getParam("dateinend");
        $dateinend = $this->_getParam("dateinbegin")>=$this->_getParam("dateinend")?$this->_getParam("dateinbegin"):$this->_getParam("dateinend");
        $result = Application_Model_Order::getOrdercount($dateinbegin,$dateinend);
        $this->view->countdata = $result;
        $view = $dateinbegin&&$dateinend?$dateinbegin." 至 ".$dateinend:($dateinbegin&&!$dateinend?$dateinbegin:(!$dateinbegin&&$dateinend?$dateinend:date("Y-m")));
        if($this->_getParam("export") == "csv"){
            header("content-Type:text/html; charset=gb2312");
            header("Content-Type: application/vnd.ms-execl");
            header("Content-Disposition: attachment; filename=" . $view . "的代购统计.csv");
            header("Pragma: no-cache");
            header("Expires: 0");
            /* first line */
            echo iconv("UTF-8","GB2312","时间,"); 
            echo iconv("UTF-8","GB2312","代购总数量,");
            echo iconv("UTF-8","GB2312","总收金额,");
            echo iconv("UTF-8","GB2312","运费,");
            echo iconv("UTF-8","GB2312","部分退款,");
            echo "\r\n";
            /* start of second line */
            if($result != ''){
                foreach ($result as $key => $Item) {
                    echo iconv("UTF-8","GB2312",date("Y-m-d",$key).",");
                    echo iconv("UTF-8","GB2312",$Item["num"].",");
                    echo iconv("UTF-8","GB2312",$Item["ysje"].",");
                    echo iconv("UTF-8","GB2312",$Item["zyf"].",");
                    echo iconv("UTF-8","GB2312",$Item["bfyk"].",");
                    echo "\r\n";
                }
            }
            exit;
        }
        $this->view->dateinbegin = $dateinbegin;
        $this->view->dateinend = $dateinend;
        $this->view->nowYm = $view;
        $this->view->stype = 9;
    }
    
    public function meetdataAction()
    {
        $this->setTitle("全局统计");
        $timescope = $this->_getParam("timescope");
        $starttime = $this->_getParam("starttime");
        $endtime = $this->_getParam("endtime");
        $strart = date("m")%3==0?date("m")/3:date("m")/3+1;
        if($timescope != null){
            switch ($timescope) {
                case 'lastweek':
                    $starttime = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-date("w")+1-7,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-date("w")+7-7,date("Y")));
                    break;
                case "week":
                    $starttime = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-date("w")+1,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-date("w")+7,date("Y")));
                    break;
                case "month":
                    $starttime =  date("Y-m-d",mktime(0,0,0,date("m"),1,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,date("m"),date("t"),date("Y")));
                    break;
                case "lastmonth":
                    $starttime = date("Y-m-d",mktime(0,0,0,date("m")-1,1,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,date("m")-1,cal_days_in_month(CAL_GREGORIAN, date("m")-1, date("Y")),date("Y")));
                    break;
                case "quarter":
                    $starttime = date("Y-m-d",mktime(0,0,0,($strart-1)*3+1,1,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,$strart*3,cal_days_in_month(CAL_GREGORIAN, $strart*3, date("Y")),date("Y")));
                    break;
                case "lastquarter":
                    $starttime = date("Y-m-d",mktime(0,0,0,($strart-1)*3-2,1,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,($strart-1)*3,cal_days_in_month(CAL_GREGORIAN, ($strart-1)*3, date("Y")),date("Y")));
                    break;
                case "year":
                    $starttime = date("Y-m-d",mktime(0,0,0,1,1,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,12,cal_days_in_month(CAL_GREGORIAN, 12, date("Y")),date("Y")));
                    break;
                case "lastyear":
                    $starttime = date("Y-m-d",mktime(0,0,0,1,1,date("Y")-1));
                    $endtime = date("Y-m-d",mktime(0,0,0,12,cal_days_in_month(CAL_GREGORIAN, 12, date("Y")-1),date("Y")-1));
                    break;
                case "day7":
                    $starttime = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-6,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
                    break;
                case "day30":
                    $starttime = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-30,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
                    break;
                default :
                    $starttime = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
                    $endtime = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-7,date("Y")));
                    break;
            }
        }
        $outstore = Application_Model_wayBillItem::getDateByBill($starttime,$endtime); //出库包裹数
        $instore = Application_Model_StoreItem::getcountinstore($starttime, $endtime);  //入库包裹数
        $instoreweight = Application_Model_StoreItem::getInStoreWeight($starttime, $endtime);//入库总重量
        $waybillMoney = Application_Model_wayBillItem::getDateByWayBill($starttime,$endtime);//出库总预付金额
        $problemWaybill = Application_Model_wayBillItem::getProblemCount($starttime,$endtime);//问题运单数量
        $shipItem = Application_Model_Order::getDateByOrder($starttime,$endtime);
        $shopNum = count($shipItem);//代购数量
        $shopMoney = 0; // 代购金额
        if($shipItem){
            foreach ($shipItem as $shipVal) {
                $shopMoney += $shipVal->sum;
            }
        }
        if($starttime == ""){
            $starttime = date("Y-m-d");
        }
        if($endtime == ""){
            $endtime = date("Y-m-d");
        }
        $this->view->timescope = $timescope;
        $this->view->starttime = $starttime;
        $this->view->endtime = $endtime;
        $this->view->outstore = $outstore;
        $this->view->instore = $instore;
        $this->view->instoreweight = $instoreweight;
        $this->view->waybillMoney = $waybillMoney;
        $this->view->problemWaybill = $problemWaybill;
        $this->view->shopNum = $shopNum;
        $this->view->shopMoney = $shopMoney;
        $this->view->stype = 11;
    }
            
    public function insandoutsAction(){
        $this->setTitle("出入库统计");
        $day = $this->getSafeParam('day');
        $dateinbegin = $this->getSafeParam("dateinbegin");
        $dateinend = $this->getSafeParam("dateinend");
        $timeArray = array();
        $incount = 0;
        $outcount = 0;
        $inweight = 0;
        $outweight = 0;
        $user = $this->getSafeParam("user");
        $user = ($user==""?Uuch_Util::getUserId():$user);
        $userItem = Application_Model_adminuser::load($user);
        if($day == "31"){
            if($dateinend > $dateinbegin){
                $user = $this->getSafeParam("selNikeName");
                $userItem = Application_Model_adminuser::load($user);
                $end = strtotime($dateinend);
                $start = strtotime($dateinbegin);
                $j = ($end - $start)/3600/24+1;
                for($i=0;$i<$j;$i++){
                    $timeArray[$i] = date("Y-m-d",$end-$i*24*3600);
                }
            }else{
                $dateinbegin = date("Y-m-d",time()-31*3600*24);
                $dateinend = date("Y-m-d");        
                for($i=0;$i<31;$i++){
                    $timeArray[$i] = date("Y-m-d",time()-$i*24*3600);
                }
            }                                
            $inDaysCount = array();             
            foreach($timeArray as $key=>$val){
                $inDaysCount[$val] = Application_Model_StoreItem::countStoreByday(1,$userItem->Nikename,$val);
                $incount += $inDaysCount[$val];
            }
            $outDaysCount = array();
            foreach($timeArray as $key=>$val){
                $outDaysCount[$val] = Application_Model_WayLog::getCountByTypeOperator(Application_Model_wayBillItem::Interior_Pick,$userItem->Nikename,  Application_Model_WayLog::Type_Bill,$val,$val);
                $outcount += $outDaysCount[$val];
            }
            $inDaysWeight = array();
            foreach($timeArray as $key=>$val){
                $inDaysWeight[$val] = Application_Model_StoreItem::weightStoreByday(1,$userItem->Nikename,$val);
                $inweight += $inDaysWeight[$val];
            }
            $outDaysWeight = array();
            foreach($timeArray as $key=>$val){
                $outDaysWeight[$val] = Application_Model_WayWeight::getSumWeightByTypeOperator(Application_Model_wayBillItem::Interior_Pick,$userItem->Nikename,Application_Model_WayLog::Type_Bill,Application_Model_WeightType::FirstWeight,$val,$val);
                $outweight += $outDaysWeight[$val];
            }
            $this->view->stype = 15;
        }else if($day == "month"){
            $num = date("d");
            for($i=0;$i<$num;$i++){
                $timeArray[$i] = date("Y-m-d",time()-$i*24*3600);
            }            
            $inDaysCount = array();        
            foreach($timeArray as $key=>$val){
                $inDaysCount[$val] = Application_Model_StoreItem::countStoreByday(3,$userItem->Nikename,$val);
                $incount += $inDaysCount[$val];
            }
            $outDaysCount = array();
            foreach($timeArray as $key=>$val){
                $outDaysCount[$val] = Application_Model_WayLog::getCountByTypeOperator(Application_Model_wayBillItem::Interior_Pick,null,Application_Model_WayLog::Type_Bill,$val,$val);
                $outcount += $outDaysCount[$val];
            }
            $inDaysWeight = array();
            foreach($timeArray as $key=>$val){
                $inDaysWeight[$val] = Application_Model_StoreItem::weightStoreByday(3,$userItem->Nikename,$val);
                $inweight += $inDaysWeight[$val];
            }
            $outDaysWeight = array();
            foreach($timeArray as $key=>$val){
                $outDaysWeight[$val] = Application_Model_WayWeight::getSumWeightByTypeOperator(Application_Model_wayBillItem::Interior_Pick,null,Application_Model_WayLog::Type_Bill,Application_Model_WeightType::FirstWeight,$val,$val);
                $outweight += $outDaysWeight[$val];
            }
            $this->view->stype = 16;
        }
        $this->view->day = $day;
        $this->view->dateinbegin = $dateinbegin;
        $this->view->dateinend = $dateinend;
        $this->view->user = $user;
        $this->view->timeArray = $timeArray;
        $this->view->inDaysCount = $inDaysCount;
        $this->view->outDaysCount = $outDaysCount;
        $this->view->inDaysWeight = $inDaysWeight;
        $this->view->outDaysWeight = $outDaysWeight;
        $this->view->userDetail = $userItem;
        $this->view->adminUserAll = Application_Model_adminuser::getAll();
        $this->view->incount = $incount;
        $this->view->outcount = $outcount;
        $this->view->inweight = $inweight;
        $this->view->outweight = $outweight;
    }
    
   
    
}
